{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-05-13T08:39:40.165382Z",
     "start_time": "2024-05-13T08:39:27.305473Z"
    }
   },
   "source": [
    "# 安装langchain\n",
    "! pip install langchain"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://mirrors.aliyun.com/pypi/simple/\n",
      "Requirement already satisfied: langchain in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (0.1.20)\n",
      "Requirement already satisfied: PyYAML>=5.3 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (6.0.1)\n",
      "Requirement already satisfied: SQLAlchemy<3,>=1.4 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (2.0.30)\n",
      "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (3.9.5)\n",
      "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (4.0.3)\n",
      "Requirement already satisfied: dataclasses-json<0.7,>=0.5.7 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (0.6.6)\n",
      "Requirement already satisfied: langchain-community<0.1,>=0.0.38 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (0.0.38)\n",
      "Requirement already satisfied: langchain-core<0.2.0,>=0.1.52 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (0.1.52)\n",
      "Requirement already satisfied: langchain-text-splitters<0.1,>=0.0.1 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (0.0.1)\n",
      "Requirement already satisfied: langsmith<0.2.0,>=0.1.17 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (0.1.57)\n",
      "Requirement already satisfied: numpy<2,>=1 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (1.26.4)\n",
      "Requirement already satisfied: pydantic<3,>=1 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (2.7.1)\n",
      "Requirement already satisfied: requests<3,>=2 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (2.31.0)\n",
      "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain) (8.3.0)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n",
      "Requirement already satisfied: attrs>=17.3.0 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.2.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.5)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4)\n",
      "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from dataclasses-json<0.7,>=0.5.7->langchain) (3.21.2)\n",
      "Requirement already satisfied: typing-inspect<1,>=0.4.0 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from dataclasses-json<0.7,>=0.5.7->langchain) (0.9.0)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain-core<0.2.0,>=0.1.52->langchain) (1.33)\n",
      "Requirement already satisfied: packaging<24.0,>=23.2 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langchain-core<0.2.0,>=0.1.52->langchain) (23.2)\n",
      "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from langsmith<0.2.0,>=0.1.17->langchain) (3.10.3)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from pydantic<3,>=1->langchain) (0.6.0)\n",
      "Requirement already satisfied: pydantic-core==2.18.2 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from pydantic<3,>=1->langchain) (2.18.2)\n",
      "Requirement already satisfied: typing-extensions>=4.6.1 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from pydantic<3,>=1->langchain) (4.11.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from requests<3,>=2->langchain) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from requests<3,>=2->langchain) (3.7)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from requests<3,>=2->langchain) (2.2.1)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from requests<3,>=2->langchain) (2024.2.2)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.3)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.2.0,>=0.1.52->langchain) (2.4)\n",
      "Requirement already satisfied: mypy-extensions>=0.3.0 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain) (1.0.0)\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-13T08:39:41.298922Z",
     "start_time": "2024-05-13T08:39:40.170763Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 开启日志追踪\n",
    "! setx LANGCHAIN_TRACING_V2 true"
   ],
   "id": "371071a487b59bc3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "成功: 指定的值已得到保存。\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-13T08:42:14.479503Z",
     "start_time": "2024-05-13T08:39:41.301925Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用llama3模型，llama3本地模型使用[ollama](https://ollama.com)安装\n",
    "from langchain_community.llms import Ollama\n",
    "llm = Ollama(model=\"llama3\")\n",
    "llm.invoke(\"how can langsmith help with testing?\")"
   ],
   "id": "99dba0dd4905422b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Languishing, in the context of software development, is a process that involves using natural language processing (NLP) techniques to test and validate human-composed texts against the expected output. This can be particularly useful for testing machine learning models, chatbots, or any other system that relies on understanding and generating text.\\n\\nHere are some ways Langsmith can help with testing:\\n\\n1. **Automated Testing**: Langsmith can automatically generate a large number of test cases by varying parameters such as sentence structure, tone, and content. This allows for comprehensive testing without the need for manual creation of test cases.\\n2. **Error Detection**: Langsmith's AI-powered algorithms can identify errors in machine learning models or chatbots by detecting inconsistencies between expected and actual outputs. This helps developers to catch bugs early on and improve their model's accuracy.\\n3. **Text Analysis**: Langsmith provides a range of text analysis tools that can be used to test the output of natural language processing (NLP) models. For example, you can use Langsmith to analyze the sentiment, tone, or entities detected in a piece of text.\\n4. **Testing Chatbots and Conversational Systems**: Langsmith's NLP capabilities allow for testing conversational systems like chatbots, voice assistants, or messaging platforms. You can simulate user interactions and test how well the system responds to different inputs.\\n5. **Testing Sentiment Analysis Models**: Langsmith can be used to test sentiment analysis models by providing a large dataset of text samples with known sentiments (e.g., positive, negative, neutral). The AI-powered algorithms in Langsmith can then analyze the output of the sentiment analysis model and identify errors or biases.\\n6. **Generating Test Data**: Langsmith can generate test data based on specific criteria, such as language, tone, or content. This helps developers to create a diverse range of test cases that cover different scenarios and edge cases.\\n\\nBy leveraging Languishing's NLP capabilities, you can streamline your testing process, reduce the risk of errors, and improve the overall quality of your machine learning models and chatbots.\""
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-13T08:43:46.657133Z",
     "start_time": "2024-05-13T08:42:14.483507Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用提示模板\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "prompt = ChatPromptTemplate.from_messages([\n",
    "    (\"system\", \"You are a world class technical documentation writer.\"),\n",
    "    (\"user\", \"{input}\")\n",
    "])\n",
    "# 组合成一个简单的 LLM 链，调用它并提出问题\n",
    "chain = prompt | llm \n",
    "chain.invoke({\"input\": \"how can langsmith help with testing?\"})"
   ],
   "id": "6e41662d4b9ebca9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"As a world-class technical documentation writer, I'd love to share some insights on how Langsmith can be leveraged for testing.\\n\\nLangsmith's AI-powered capabilities can significantly enhance the testing process by:\\n\\n1. **Automated Testing**: Langsmith can assist in automating repetitive and time-consuming tasks, such as data validation, formatting, and consistency checks.\\n2. **Test Data Generation**: The AI engine can generate test data sets based on predefined patterns, formats, or templates, reducing the need for manual data creation.\\n3. **Error Detection**: Langsmith's natural language processing (NLP) capabilities enable it to detect errors in syntax, grammar, formatting, and consistency, making it an excellent tool for catching mistakes that might slip through human testing.\\n4. **Test Case Development**: The AI can help create test cases by generating scenarios based on the documentation's content, ensuring comprehensive coverage of possible use cases.\\n5. **Test Reporting**: Langsmith can summarize test results in a clear and concise manner, making it easier to identify and track defects.\\n6. **Collaboration**: By integrating with popular testing tools and platforms, Langsmith enables seamless collaboration among teams, facilitating the testing process across different stages.\\n\\nTo get started, you can feed Langsmith your documentation, and let its AI engine do the heavy lifting! What specific areas of testing would you like to focus on?\""
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-13T08:46:34.580024Z",
     "start_time": "2024-05-13T08:43:46.663673Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 添加一个简单的输出解析器\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "output_parser = StrOutputParser()\n",
    "# 组合成一个简单的 LLM 链，调用它并提出问题\n",
    "chain = prompt | llm | output_parser\n",
    "chain.invoke({\"input\": \"how can langsmith help with testing?\"})"
   ],
   "id": "27a2554e6d7caccf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"As a world-class technical documentation writer, I'm delighted to share some insights on how Langsmith can help with testing.\\n\\nLangsmith's AI-powered translation and localization capabilities can significantly streamline the testing process for your software applications, especially when it comes to internationalization (i18n) and localization (L10n). Here are some ways Langsmith can assist:\\n\\n1. **Automated Translation Testing**: Langsmith can translate your test cases, scenarios, or user stories into multiple languages, allowing you to test your application's functionality and user experience in different linguistic and cultural contexts.\\n2. **Localization Validation**: With Langsmith, you can generate localized versions of your test data (e.g., user interfaces, error messages, and help texts) to ensure that your application behaves correctly in various regions and markets.\\n3. **Cultural and Linguistic Analysis**: By analyzing the translations generated by Langsmith, you can identify potential cultural or linguistic nuances that might impact your application's usability, accessibility, or overall performance.\\n4. **Testing for Non-English Language Support**: If your software supports non-English languages, Langsmith can help you test the functionality of these features by translating test cases and data into those languages.\\n5. **Reduced Testing Time and Effort**: By automating the translation and localization process, you'll save time and effort that would have been spent on manual testing, allowing your team to focus on more complex testing scenarios or other critical tasks.\\n\\nTo get started with Langsmith's testing assistance, I recommend exploring their APIs, plugins, or integrations with popular testing frameworks. You can also leverage Langsmith's translation memory features to store and reuse translations across different tests, projects, or even applications.\\n\\nHow would you like to proceed? Would you like me to provide more information on Langsmith's specific features or tools that can support your testing needs?\""
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
